class HoverObserver {
    constructor() {
        this.hoverSelectors = new Set();
        this.hoverRuleCache = new WeakMap();
        this.pendingChecks = new Set();
        this.checkTimer = null;
        this.collectHoverSelectors();
        this.setupEventListeners();
    }
    
    collectHoverSelectors() {
        for (const styleSheet of document.styleSheets) {
            try {
                const rules = styleSheet.cssRules || styleSheet.rules;
                if (rules) {
                    for (const rule of rules) {
                        if (rule.type === CSSRule.STYLE_RULE && 
                            rule.selectorText && 
                            rule.selectorText.includes(':hover')) {
                            const baseSelector = rule.selectorText.split(':hover')[0].trim();
                            this.hoverSelectors.add(baseSelector);
                        }
                    }
                }
            } catch (e) {
                console.warn('无法读取样式表规则', e);
            }
        }
    }
    
    setupEventListeners() {
        document.addEventListener('mouseenter', (e) => {
            this.queueElementCheck(e.target);
        }, true);
    }
    
    queueElementCheck(element) {
        if (!element || !element.tagName) return;
        
        this.pendingChecks.add(element);
        
        if (!this.checkTimer) {
            this.checkTimer = requestAnimationFrame(() => {
                this.processPendingChecks();
                this.checkTimer = null;
            });
        }
    }
    
    processPendingChecks() {
        const elements = [...this.pendingChecks];
        this.pendingChecks.clear();
        
        for (const element of elements) {
            if (this.checkElementHasHover(element)) {
                this.handleHoverElement(element);
            }
        }
    }
    
    checkElementHasHover(element) {
        if (this.hoverRuleCache.has(element)) {
            return this.hoverRuleCache.get(element);
        }
        
        const selectors = this.generateElementSelectors(element);
        const hasHover = selectors.some(sel => this.hoverSelectors.has(sel));
        
        this.hoverRuleCache.set(element, hasHover);
        return hasHover;
    }
    
    generateElementSelectors(element) {
        const tag = element.tagName.toLowerCase();
        const id = element.id ? `#${element.id}` : '';
        const classes = element.classList ? Array.from(element.classList) : [];
        
        const selectors = [tag, id];
        classes.forEach(c => selectors.push(`.${c}`));
        
        // 生成组合选择器（最多3个类组合）
        if (classes.length > 1) {
            for (let i = 0; i < classes.length; i++) {
                for (let j = i + 1; j < Math.min(i + 3, classes.length); j++) {
                    selectors.push(`.${classes[i]}.${classes[j]}`);
                }
            }
        }
        
        // 生成带标签的组合
        const withTag = selectors.filter(s => !s.startsWith('.')).map(s => `${tag}${s}`);
        selectors.push(...withTag);
        
        return selectors;
    }
    
    handleHoverElement(element) {
        // 可自定义的hover处理逻辑
        console.log(`Hover on ${element.tagName}`, element);
    }
}

// 使用
const hoverObserver = new HoverObserver();